<template>
  <div class="monthlyStatement">
    <!-- 月结操作 -->
    <div class="new_vs">
      <div class="vs-top">
        <p>月结操作</p>
        <!-- <i class="el-icon-close"></i> -->
      </div>
      <div class="msg">
        <p>截止日期</p>
        <el-date-picker v-model="date" type="date" value-format="yyyy-MM-dd">
        </el-date-picker>
        <p>密码</p>
        <el-input
          ref="pswInput"
          v-model="psw"
          @keyup.enter.native="settleAccounts"
        ></el-input>
      </div>
      <div class="ms_content">
        <el-button
          @click="determine"
          type="primary"
          icon="el-icon-search"
          style="margin-right: 222px"
          >月结</el-button
        >
        <el-checkbox style="margin: 0 0 0 10px" v-model="vendor" border
          >厂进退</el-checkbox
        >
        <el-checkbox style="margin: 0 0 0 10px" v-model="customer" border
          >客进退销</el-checkbox
        >

        <el-progress
          :text-inside="true"
          :stroke-width="20"
          :percentage="speed"
          style="margin-top: 10px"
        ></el-progress>
        <i class="el-icon-question">
          提示：点“月结”后，在密码框填改单密码并按回车，等待进度条完成！</i
        >
        <el-table
          :data="tableData"
          height="300"
          style="width: 100%"
          :header-cell-style="{
            background: '#f4f4f4',
            padding: 0,
            height: '40px'
          }"
          :cell-style="{ padding: 0, height: '36px' }"
        >
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="expirationTime" label="截止日期" width="160">
          </el-table-column>
          <el-table-column prop="enteringTime" label="日期" width="160">
          </el-table-column>
          <el-table-column prop="operatory" label="操作员"> </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import { list, monthly, getMaxTime } from '../../../network/monthlyInfo'
import { internetProtocol } from '../../../config/ip'

import today from '../../../components/mixins/today'

export default {
  name: 'monthly_statement',
  mixins: [today],
  data() {
    return {
      date: this.searchDate(0),
      psw: '',
      vendor: false,
      customer: false,
      tableData: [],
      speed: 0,
      divider: ''
    }
  },
  created() {
    getMaxTime().then((res) => {
      // console.log(res)
      this.divider = res.data.data
    })
    this.search()
  },
  methods: {
    determine() {
      if (this.compareTime(this.date)) {
        this.$confirm(`是否月结${this.date}之前的数据`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$refs.pswInput.focus()
        })
      }
    },
    settleAccounts() {
      if (!this.psw) {
        this.$alert('密码不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.compareTime(this.date)) return
      let ws = new WebSocket(
        `wss://${internetProtocol}/webSocket/${
          document.cookie.match(RegExp('jxUser' + '=([^;]+)'))[1]
        }`
      )
      ws.onopen = (e) => {
        // console.log(e)
      }
      ws.onmessage = (e) => {
        // console.log(e)
        // console.log(e.data)
        if (e.data != '连接成功') {
          this.speed = e.data * 10
        }
      }
      ws.onclose = () => {
        // console.log("调转连接已关闭...")
      }
      ws.onerror = (e) => {
        console.log(e)
      }
      monthly({ time: this.date }).then((res) => {
        ws.close()
        // console.log(res)
        if (res.data.code == 200) {
          this.$alert('单据月结完成', '', {
            confirmButtonText: '确定'
          })
          this.search()
        }
      })
    },
    search() {
      list().then((res) => {
        // console.log(res)
        if (res.data.code == 200) {
          this.tableData = res.data.data
          this.tableData.forEach((e) => {
            e.enteringTime = e.enteringTime.slice(0, 10)
            e.expirationTime = e.expirationTime.slice(0, 10)
          })
        }
      })
    },
    compareTime(a) {
      // console.log(a)
      // console.log(Date.parse(a))
      // console.log(Date.parse(this.divider))
      // console.log(this.divider)
      if (Date.parse(a) < Date.parse(this.divider)) {
        this.$alert('选择时间已月结过', '', {
          confirmButtonText: '确定'
        })
        return false
      }
      return true
    }
  }
}
</script>

<style scoped>
.monthlyStatement {
  margin-right: 0.5vw;
  padding: 0 15px;
  background-color: white;
  height: 95vh;
  position: relative;
}
.monthlyStatement .new_vs {
  position: fixed;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  width: 520px;
  padding-bottom: 10px;
  z-index: 99;
  background-color: white;
  box-shadow: 0 0 5px #aaaaaa;
}
.monthlyStatement .new_vs .vs-top {
  height: 40px;
  background-color: #f8f8f8;
}
.monthlyStatement .new_vs .vs-top p {
  font-weight: 700;
  float: left;
  line-height: 40px;
  padding-left: 20px;
}
.monthlyStatement .new_vs .vs-top i {
  float: right;
  line-height: 40px;
  padding: 0 16px;
}
.monthlyStatement .msg {
  border-bottom: 2px solid #f4f4f4;
  padding: 10px;
  margin-bottom: 10px;
}
.monthlyStatement .msg p {
  display: inline-block;
  width: 75px;
  text-align: right;
  margin-right: 10px;
}

.monthlyStatement .ms_content {
  padding: 0 10px;
}
.monthlyStatement .ms_content i {
  color: #017aff;
  font-size: 15px;
  margin: 10px 0;
}
</style>

<style>
.monthlyStatement .el-input__icon {
  line-height: 10px;
}
.monthlyStatement .el-input {
  display: inline;
}
.monthlyStatement .el-input__inner {
  height: 30px;
  width: 160px;
}
.monthlyStatement .el-checkbox.is-bordered {
  padding: 4px 5px 0 5px;
  height: 30px;
}
</style>
